package xmwxapplet

import (
	"github.com/gin-gonic/gin"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/dao/memberdao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/membermodel"
	"yundian/internel/app/models/reports"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// HandleInsertPlaceReport 门店投诉建议
func HandleInsertPlaceReport(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		Response(ctx)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}

	param := &request.InsertPlaceReportReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId = userId
	param.Status = helper.TypeNumPlaceReportsStatusWait

	Response(ctx)("", CodeInsertSuccess, dao.NewReportDao().InsertPlaceReport(param).Unwrap())(OK)
}

// ------------故障相关请求处理---------------//

// ReportInsert 微信小程序故障提交
func HandleInsertReport(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		Response(ctx)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}
	param := reports.New()
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	memberInfo := memberdao.NewMemberDao().GetMemberInfoById(userId).Unwrap().(*membermodel.MembersModel)
	param.MemberId = userId
	param.Status = 1
	param.IsOrder = 1
	param.CreateAt = dingo.Time(time.Now().Unix())
	if param.Phone == "" {
		param.Phone = memberInfo.Phone
	}
	//log.Println("提交上来的参数", param)

	Response(ctx)("", CodeInsertSuccess, dao.NewReportDao().InsertReport(param).Unwrap())(OK)
}

// ComplaintAdvice 微信小程序投诉建议提交
func HandleInsertComplaintAdvice(c *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(c)
	if err != nil {
		Response(c)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}
	//userId := 51
	param := reports.New()
	dingo.ErrorIf(c.ShouldBindJSON(param)).Unwrap()
	memberInfo := memberdao.NewMemberDao().GetMemberInfoById(userId).Unwrap().(*membermodel.MembersModel)
	param.MemberId = userId
	param.Status = 1
	param.IsOrder = 2
	param.CreateAt = dingo.Time(time.Now().Unix())
	if param.Phone == "" {
		param.Phone = memberInfo.Phone
	}
	Response(c)("", CodeInsertSuccess, dao.NewReportDao().InsertReport(param).Unwrap())(OK)
}
